import md5 from 'js-md5';
import { updateLoginInfo, getLoginInfo } from '@/api/user/userSetting'
import df from '@/utils/df'
import tools from '@/utils/tools'
import { mapGetters } from 'vuex'
import { setIsLogin } from '@/utils/auth'
const basic = tools.basic
export default {
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('新密码不能为空'));
      } else {
        if (this.dataForm.cfjkymm !== '') {
          this.$refs.dataForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.dataForm.jkymm) {
        callback(new Error('两次密码输入不一致'));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      formLoading: false,
      btnLoading: false,
      dataForm: {
        jkydm: '',
        jkymm: '',
        cfjkymm: '',
        jkyxm: '',
        jkylxdh: ''
      },
      dataRule: {
        jkyxm: [
          { required: true, message: '请输入监考员姓名', trigger: 'blur' }
        ],
        jkymm: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        cfjkymm: [
          { required: true, message: '重复密码不能为空', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        jkylxdh: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
      }
    }
  },
  created() {

  },
  methods: {
    init() {
      this.visible = true
      this.formLoading = true
      this.$nextTick(() => {
        this.listLoading = false
      })
      this.formLoading = false
      this.getUserLoginInfo()
      this.dataForm = basic.partData((this.userInfo), [
        'jkydm',
        'jkymm',
        { cfjkymm: 'jkymm' },
        'jkyxm',
        'jkylxdh'
      ])
    },
    goBack() {
      this.$router.back()
    },
    onClose () {
      this.visible = false
      this.$emit('close')
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          /*{
            oldPassword: this.dataForm.oldPassword,
            password: this.dataForm.password//md5(this.dataForm.password),
          }*/
          updateLoginInfo(this.dataForm).then(res => {
            this.$message({
              message: res.Result.Msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                setIsLogin (true)
                this.$router.push({
                  path: "/home"
                });
              }
            })
          })
        }
      })
    },
    getUserLoginInfo() {
      this.dataForm = this.$store.getters.userInfo
      this.dataForm.yhmm = ''
      // console.log(this.$store.getters.userInfo)
      /*getLoginInfo(df.storageGet('loginInfo')).then(res => {
        this.dataForm = basic.partData(res.Response, [
          'yhdm',
          'mm',
          'cfmm',
          'yhmc',
          'lxdh'])
      })*/
    },
    changeImg() {
      let timestamp = Math.random()
      this.timestamp = timestamp
      this.imgUrl = `/api/file/ImageCode/${timestamp}`
    }
  }
}

